import logging
from datetime import datetime

from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to
from pylons.decorators.rest import restrict

from sc_console.lib.base import BaseController, render

from sc_console.model.models import RawMessage
from sc_console.model.meta import Session

from sc_console.lib.base import BaseController

log = logging.getLogger(__name__)

class RawMessageController(BaseController):
    def index(self):
        return render('/raw_message/index.mako')

    def _create_raw_message(self, name, data):
        rm = RawMessage(message_name=name, data=data)

        Session.add(rm)
        Session.commit()

    @restrict('POST')
    def player_data_requested(self):
        id = request.POST['player_id']

        try:
            id = int(id)
        except ValueError as e:
            c.error_msg = 'ID must be an integer. You entered "%s"' % id
            return render('/raw_message/error.mako')

        message_name = 'sc_engine.apps.messages.PlayerDataRequested'
        data = dict(player_id = id)

        self._create_raw_message(message_name, data)
        return redirect_to('/raw_message/successful')

    @restrict('POST')
    def player_alias_addition(self):
        id = request.POST['player_id']
        alias = request.POST['alias']

        try:
            id = int(id)
        except ValueError as e:
            c.error_msg = 'ID must be an integer. You entered "%s"' % id
            return render('/raw_message/error.mako')
        
        if len(alias) == 0:
            c.errorMsg = 'Must enter an alias.'
            return render('/raw_message/error.mako')

        message_name = 'sc_engine.apps.messages.PlayerAliasAddition'
        data = dict(player_id=id, aliases=alias)

        self._create_raw_message(message_name, data)
        return redirect_to('/raw_message/successful')

    @restrict('POST')
    def player_name_determination(self):
        id = request.POST['player_id']
        name = request.POST['name']

        try:
            id = int(id)
        except ValueError as e:
            c.error_msg = 'ID must be an integer. You entered "%s"' % id
            return render('/raw_message/error.mako')
        
        if len(name) == 0:
            c.errorMsg = 'Must enter a name.'
            return render('/raw_message/error.mako')

        message_name = 'sc_engine.apps.messages.PlayerNameDetermination'
        data = dict(player_id=id, true_name=name)

        self._create_raw_message(message_name, data)
        return redirect_to('/raw_message/successful')

    @restrict('POST')
    def proleague_schedule_fetch(self):
        date = request.POST['date']
        error_msg = 'Date must be in the form YYYYMMDD. You entered "%s"' % date

        if len(date) != 8:
            c.error_msg = error_msg
            return render('/raw_message/error.mako')

        try:
            date = datetime.strptime(date, '%Y%m%d')
        except ValueError as e:
            c.error_msg = error_msg
            return render('/raw_message/error.mako')

        message_name = 'sc_engine.apps.messages.ProleagueFetchRequested'
        data = dict(date=date)

        self._create_raw_message(message_name, data)
        return redirect_to('/raw_message/successful')


    def successful(self):
        return render('/raw_message/successful.mako')

